<?php
include('../../includes/classes/core.php');

if($_REQUEST['act'] == 'get_list'){
    $count = 		$_REQUEST['count'];
    $hidden = 		$_REQUEST['hidden'];
    $start =        $_REQUEST['start'];
    $end =          $_REQUEST['end'];
    $rResult = mysql_query("SELECT 	persons.`name` AS `oper`,
                                    persons.`name` AS `oper`,
                    				COUNT(task_detail.id) as `total_call`,
                    				ROUND((COUNT(task_detail.id) / (
                    					SELECT COUNT(task_detail.id) 
                    					FROM task_detail 
                    					JOIN		task_scenar ON task_detail.id = task_scenar.task_detail_id
                    					WHERE 	task_detail.`status` IN(2,3,4) 
                    					AND DATE(task_scenar.date) >= '$start' 
                    					AND DATE(task_scenar.date) <= '$end'
                    				) *100),2) AS `total_call_proc`,
                    				(
                    					SELECT 	COUNT(DISTINCT out_call.phone) as `done_call`
                    					FROM 		task_detail AS `gg`
                                        JOIN `phone` ON `gg`.`phone_base_id` = `phone`.`id`
                                        JOIN `out_call` ON `phone`.`phone1` = `out_call`.`phone` AND out_call.`status` = 'ANSWERED' AND out_call.`duration` > 15
                    					JOIN		task_scenar ON gg.id = task_scenar.task_detail_id
                    					JOIN 		users ON gg.responsible_user_id = users.id
                    					JOIN 		persons ON users.person_id = persons.id
                    					WHERE 	task_detail.responsible_user_id = gg.responsible_user_id AND gg.`status` IN(3) AND DATE(task_scenar.date) >= '$start' AND DATE(task_scenar.date) <= '$end'
                    					LIMIT 1
                    				) AS `done_call`,
                    				ROUND((
                    				(
                    					SELECT 	COUNT(DISTINCT out_call.phone) as `done_call`
                    					FROM 		task_detail AS `gg`
                                        JOIN `phone` ON `gg`.`phone_base_id` = `phone`.`id`
                                        JOIN `out_call` ON `phone`.`phone1` = `out_call`.`phone` AND out_call.`status` = 'ANSWERED' AND out_call.`duration` > 15
                    					JOIN		task_scenar ON gg.id = task_scenar.task_detail_id
                    					JOIN 		users ON gg.responsible_user_id = users.id
                    					JOIN 		persons ON users.person_id = persons.id
                    					WHERE 	task_detail.responsible_user_id = gg.responsible_user_id AND gg.`status` IN(3) AND DATE(task_scenar.date) >= '$start' AND DATE(task_scenar.date) <= '$end'
                    					LIMIT 1
                    				)
                    				/ 			
                    				(
                    					SELECT 	COUNT(DISTINCT out_call.phone) as `done_call`
                    					FROM 		task_detail AS `gg`
                                        JOIN `phone` ON `gg`.`phone_base_id` = `phone`.`id`
                                        JOIN `out_call` ON `phone`.`phone1` = `out_call`.`phone` AND out_call.`status` = 'ANSWERED' AND out_call.`duration` > 15
                    					JOIN		task_scenar ON gg.id = task_scenar.task_detail_id
                    					JOIN 		users ON gg.responsible_user_id = users.id
                    					JOIN 		persons ON users.person_id = persons.id
                    					WHERE 	gg.`status` IN(3) AND DATE(task_scenar.date) >= '$start' AND DATE(task_scenar.date) <= '$end'
                    					LIMIT 1
                    				) *100),2) AS `done_call_proc`,
                    				(
                    					SELECT 	COUNT(DISTINCT out_call.phone) as `done_call`
                    					FROM 		task_detail AS `gg`
                                        JOIN `phone` ON `gg`.`phone_base_id` = `phone`.`id`
                                        JOIN `out_call` ON `phone`.`phone1` = `out_call`.`phone` AND out_call.`status` = 'ANSWERED' AND out_call.`duration` > 15
                    					JOIN		task_scenar ON gg.id = task_scenar.task_detail_id
                    					WHERE 	task_detail.responsible_user_id = gg.responsible_user_id 
                    					AND gg.`status` IN(3) 
                    					AND DATE(task_scenar.date) >= '$start' 
                    					AND DATE(task_scenar.date) <= '$end'
                    					AND ((task_scenar.status_1 = 1 AND task_scenar.status_2 = 1) OR task_scenar.status_3 = 1)
                    					LIMIT 1
                    				) AS `miz`,
                    				ROUND((
                    				(
                    					SELECT 	COUNT(DISTINCT out_call.phone) as `done_call`
                    					FROM 	task_detail AS `gg`
                    					JOIN	task_scenar ON gg.id = task_scenar.task_detail_id
                                        JOIN `phone` ON `gg`.`phone_base_id` = `phone`.`id`
                                        JOIN `out_call` ON `phone`.`phone1` = `out_call`.`phone` AND out_call.`status` = 'ANSWERED' AND out_call.`duration` > 15
                    					WHERE 	task_detail.responsible_user_id = gg.responsible_user_id 
                    					AND gg.`status` IN(3) 
                    					AND DATE(task_scenar.date) >= '$start' 
                    					AND DATE(task_scenar.date) <= '$end'
                    					AND ((task_scenar.status_1 = 1 AND task_scenar.status_2 = 1) OR task_scenar.status_3 = 1)
                    					LIMIT 1
                    				)
                    				/ 			
                    				(
                    					SELECT 	COUNT(DISTINCT out_call.phone) as `done_call`
                    					FROM 	task_detail AS `gg`
                    					JOIN	task_scenar ON gg.id = task_scenar.task_detail_id
                                        JOIN `phone` ON `gg`.`phone_base_id` = `phone`.`id`
                                        JOIN `out_call` ON `phone`.`phone1` = `out_call`.`phone` AND out_call.`status` = 'ANSWERED' AND out_call.`duration` > 15
                    					WHERE 	gg.`status` IN(3) 
                    					AND DATE(task_scenar.date) >= '$start' 
                    					AND DATE(task_scenar.date) <= '$end'
                    					AND ((task_scenar.status_1 = 1 AND task_scenar.status_2 = 1) OR task_scenar.status_3 = 1)
                    					LIMIT 1
                    				) *100),2) AS `miz_proc`
                            FROM 	task_detail
                            JOIN	task_scenar ON task_detail.id = task_scenar.task_detail_id
                            JOIN 	users ON task_detail.responsible_user_id = users.id
                            JOIN 	persons ON users.person_id = persons.id
                            WHERE 	task_detail.`status` IN(2,3,4) AND DATE(task_scenar.date) >= '$start' AND DATE(task_scenar.date) <= '$end'
                            GROUP BY task_detail.responsible_user_id");
     
    $data = array(
        "aaData"	=> array()
    );
    
    while ( $aRow = mysql_fetch_array( $rResult ) )
    {
        $row = array();
        for ( $i = 0 ; $i < $count ; $i++ )
        {
            /* General output */
            $row[] = $aRow[$i];
        }
        $data['aaData'][] = $row;
    }
    
    echo json_encode($data);
}

if($_REQUEST['act'] == 'chart1'){
    header('Content-Type: application/json');
    $start = $_REQUEST['start'];
    $end   = $_REQUEST['end'];
    
    $quantity = array();
    $cause = array();
    $cause1 = array();
    
    $name = array();
    $agent = array();
    
    $ress =mysql_query("SELECT 	persons.`name` AS `operat`,
                				COUNT(DISTINCT out_call.phone) as `done_call`
                        FROM 	task_detail
                        JOIN `phone` ON `task_detail`.`phone_base_id` = `phone`.`id`
                        JOIN `out_call` ON `phone`.`phone1` = `out_call`.`phone` AND out_call.`status` = 'ANSWERED' AND out_call.`duration` > 15
                        JOIN	task_scenar ON task_detail.id = task_scenar.task_detail_id
                        JOIN 	users ON task_detail.responsible_user_id = users.id
                        JOIN 	persons ON users.person_id = persons.id
                        WHERE 	task_detail.`status` IN(3) AND DATE(task_scenar.date) >= '$start' AND DATE(task_scenar.date) <= '$end'
                        GROUP BY task_detail.responsible_user_id");
         
        while($row1 = mysql_fetch_assoc($ress)){
    
        $call_count[]   = (float)$row1[done_call];
        $agent[]		= $row1[operat];
    }
    
    $unit=" ზარი";
    $series[] = array('name' => $name, 'unit' => $unit, 'call_count' => $call_count, 'agent' => $agent);
    
    echo json_encode($series);
}

if($_REQUEST['act'] == 'chart2'){
    header('Content-Type: application/json');
    $start = $_REQUEST['start'];
    $end   = $_REQUEST['end'];

    $quantity = array();
    $cause = array();
    $cause1 = array();

    $name = array();
    $agent = array();

    $ress =mysql_query("SELECT 	persons.`name` AS `operat`,
                                COUNT(DISTINCT out_call.phone) as `done_call`
                        FROM 	task_detail
                        JOIN `phone` ON `task_detail`.`phone_base_id` = `phone`.`id`
                        JOIN `out_call` ON `phone`.`phone1` = `out_call`.`phone` AND out_call.`status` = 'ANSWERED' AND out_call.`duration` > 15
                        JOIN	task_scenar ON task_detail.id = task_scenar.task_detail_id
                        JOIN 	users ON task_detail.responsible_user_id = users.id
                        JOIN 	persons ON users.person_id = persons.id
                        WHERE 	task_detail.`status` IN(3)
                        AND DATE(task_scenar.date) >= '$start'
                        AND DATE(task_scenar.date) <= '$end'
                        AND ((task_scenar.status_1 = 1 AND task_scenar.status_2 = 1) OR task_scenar.status_3 = 1)
                        GROUP BY task_detail.responsible_user_id");
         
        while($row1 = mysql_fetch_assoc($ress)){

        $call_count[]   = (float)$row1[done_call];
        $agent[]		= $row1[operat];
        }

        $unit=" ზარი";
        $series[] = array('name' => $name, 'unit' => $unit, 'call_count' => $call_count, 'agent' => $agent);

            echo json_encode($series);
}

if($_REQUEST['act'] == 'chart'){
    header('Content-Type: application/json');
    $start = $_REQUEST['start'];
    $end   = $_REQUEST['end'];

    $quantity = array();
    $cause = array();
    $cause1 = array();

    $name = array();
    $agent = array();

    $ress =mysql_query("SELECT 	persons.`name` AS `operat`,
                				COUNT(task_detail.id) as `done_call`
                        FROM 	task_detail
                        JOIN	task_scenar ON task_detail.id = task_scenar.task_detail_id
                        JOIN 	users ON task_detail.responsible_user_id = users.id
                        JOIN 	persons ON users.person_id = persons.id
                        WHERE 	task_detail.`status` IN(2,3,4) AND DATE(task_scenar.date) >= '$start' AND DATE(task_scenar.date) <= '$end'
                        GROUP BY task_detail.responsible_user_id");
     
    while($row1 = mysql_fetch_assoc($ress)){

        $call_count[]   = (float)$row1[done_call];
        $agent[]		= $row1[operat];
    }

    $unit[]=" ზარი";
    $series[] = array('name' => $name, 'unit' => $unit, 'call_count' => $call_count, 'agent' => $agent);

    echo json_encode($series);
}
?>